package com.etop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.etop.basic.controller.BaseController;

import com.etop.pojo.Function;
import com.etop.service.FunctionService;
import com.etop.service.PermissionService;
import com.etop.service.RoleService;

/**
 * 类名:FunctionController 
 * 描述:处理资源管理的controller 
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/function")
public final class FunctionController extends BaseController {
     private final static Logger logger=Logger.getLogger(FunctionController.class);
     Map<String,Object> map=new HashMap<String,Object>();
     @Resource
     private FunctionService functionService;
     @Resource 
     private RoleService roleService;
     @Resource
     private PermissionService permissionService;
     
     @RequestMapping("/functionList.html")
     public String  getFunctions(){
    	 logger.info("/functionList.html");
    	 return "/functionList";
     }
     
     /**
      * 类名:getFunctionList
      *     返回分页的json数据
      * @author Administrator
      */
     @RequestMapping("/get_functions.html")
     @ResponseBody
     public Object getFunctionList(){
    	 logger.info("/get_functions.html");
    	 List<Function> list=functionService.findAll();
    	 return list;
     }
     /**
      * 类名:addFunction
      * @author Administrator    
      */
     @RequestMapping("/add.html")
     @ResponseBody
     public Object addFunction(String value,
    		                   int permission_id,
    		                   int role_id,
    		                   String type){
    	 logger.info("/add.html");
    	 Function function=new Function();
    	 function.setPermission_id(permission_id);
    	 function.setRole_id(role_id);
    	 function.setType(type);
    	 function.setValue(value);
    	 functionService.saveFunction(function);
    	 map.put(SUCCESS, true);
    	 return map;
     }
     @RequestMapping("/edit.html")
     @ResponseBody
     public Object updateFunction(int id,
    		                      String value,
    		                      String type){
    	 logger.info("/edit.html");
    	 Function function=functionService.findFunctionById(id);
    	 if(function==null||"".equals(function)){
    		 map.put(ERROR, true);
    		 return map;
    	 }
    	 function.setType(type);
    	 function.setValue(value);
    	 functionService.updateFunction(function);
    	 map.put(SUCCESS, true);
    	 return map;
     }
     @RequestMapping("/del.html")
     @ResponseBody
     public Object deleteFunction(int id){
    	 logger.info("/del.html");
    	 functionService.deleteFunction(id);
    	 map.put(SUCCESS, true);
    	 return map;
     } 
}
